'''
Created on Apr 23, 2014

@author: kieubinh
'''
linkInput = "data\RITE2-UnitTest\input\RITE2_JA_dev_unittest\RITE2_JA_dev_unittest.parsed.KNP.xml"
linkOutput="data\RITE2-UnitTest\\features\\feature_train.txt"

import os
directory_path = os.getcwd()

import sys
sys.path.append('../ReadData/')
import xml2dict


def sameToken(token1, token2):
    print token1['Token'][0]['orig'], token2['Token'][0]['orig']
    if token1['Token'][0]['orig']==token2['Token'][0]['orig']:
        return True
    else:
        return False
    

if __name__ == '__main__':
    # get root path to ../javn/
    root_path = directory_path[:directory_path.find("javn")+5]
        
    inputFile = root_path+linkInput
    outputFile = root_path+linkOutput
    
    xml = xml2dict.XML2Dict()
    
    doc = xml.parse(inputFile)
    
    #print outputFile
    out = open(outputFile,'w')
    # extract Cosine similarity of content words
    for pair in doc['dataset']['pair']:
        #get token t1, t2
        token_t1 = []
        token_t2 = []
        #print len(pair['t1']['Sentence']['Chunk'])
        for token in pair['t1']['Sentence']['Chunk']:
            #print token
            token_t1.append(token)
        for token in pair['t2']['Sentence']['Chunk']:
            token_t2.append(token)
        #print len(token_t1), len(token_t2)
        similar_token=0
        for i in range(len(token_t1)):
            #print token_t1[i]
            for j in range(len(token_t2)):
                if sameToken(token_t1[i], token_t2[j]):
                    similar_token+=1
                continue
        cosine_words=1.0*min(similar_token)/(len(token_t1)*len(token_t2))
                    
        